﻿@model BuyThePeople.Models.Goods
@using BuyThePeople


@{
    ViewBag.Title = "Detail";
    var data = Session[BuyThePeople.ConstFactory.SessionUser] is BuyThePeople.Models.UserLoginData ? Session[BuyThePeople.ConstFactory.SessionUser] as BuyThePeople.Models.UserLoginData : new BuyThePeople.Models.UserLoginData();
    ViewBag.Url = ConstFactory.UrlGoodsDetail + ViewBag.Id;
}

    <div class="main_container">
		<div id="content">
			<div style="margin:25px;">
                <div style="float:left;">
				    <div style="height:180px;width:180px;text-align:center;vertical-align:middle;">
					    <!-- 상품이미지 -------------------------------------------------------------------------------------------->
					    <!-- 상품이미지 -------------------------------------------------------------------------------------------->
					    <!-- 상품이미지 -------------------------------------------------------------------------------------------->
                        <img src="@Model.goods.Image" alt="상품이미지"  style="height:180px; width:180px;">
				    </div>
                    <div style="text-align:center;">
                        @{
                            var join = Math.Ceiling((Model.goods.JoinDateEnd.Value - new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day)).TotalDays) < 0 ? 0 : Math.Ceiling((Model.goods.JoinDateEnd.Value - new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day)).TotalDays);
                            var bid = Math.Ceiling((Model.goods.BidDateEnd.Value - new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day)).TotalDays) < 0 ? 0 : Math.Ceiling((Model.goods.BidDateEnd.Value - new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day)).TotalDays);
                        }
                        <span style="color:#ffffff;font-size:18pt;;background-color:#000000;line-height:40px;">@join</span>
                        <span style="font-size:16pt;">일남음</span>
                    </div>
                    <div style="text-align:center;height:30px;">      
                        <span style="color:#22bb90;font-size:16pt;line-height:40px;"><strong><span id="joinCount">@Model.count</span></strong></span>
                        <span style="font-size:16pt;">명참여</span>
				    </div>
                </div>
				<div class="price"  style="float:left; width:665px; height:270px; background-color:#fafafa;border-radius:10px;">
					<div style="height:80px;margin:15px;">
						<a href="@Model.goods.Link" target="_blank"><p><span>인터넷최저가 <strong>보러가기</strong></span></p></a>
						<span style="font-size:17pt;line-height:100%;">@Html.DisplayFor(model => model.goods.Title)</span>
					</div>
                    <div>
						<span><span id="detailprice" ><span id="price_min">@string.Format("{0:N0}", Math.Truncate((double)(Model.goods.LowPrice * (1 - (0.03 * Model.count / 100))) / 10.0) * 10)</span>원~<span id="price_max">@string.Format("{0:N0}", Model.goods.LowPrice)원</span></span> <abbr title="최저가격 = 최고가격 - 공구원 * 0.03%" class="initalism"><code>?</code></abbr></span>
					</div>                    
					<div style="margin:10px;height:30px;">

                        @*공구종료일자와 오늘 비교하여 보이고 안보이고 제어*@
                        @if (join > 0)
                        {
						<div style="float:left;">
							<button class="btn btn-midium btn-primary" type="button" id="join">공구참여</button>							
						</div>
                        }
                        else
                        {
							    <div>공동구매가 종료되었습니다.</div>
                        }
                        
                        @if (data != null)
                        {
                            if (data.IsSeller)
                            {
                                @*입찰종료일자와 오늘 비교하여 보이고 안보이고 제어*@
                                if (bid > 0)
                                {
						        <!--입찰메뉴---------------------------------------------------->
						        <div style="float:left;margin-left:5px;">
							        <button id="bid" class="btn btn-midium btn-success" type="button">입찰참여</button>
						        </div>						
						        <div style="float:left;margin-left:5px;">
							        <input id="price" type="text" placeholder="입찰 금액을 입력하세요.">
							        <span style="font-size:13pt;">원</span>					 
						        </div>					
						        <!--입찰메뉴---------------------------------------------------->
                                }
                                else
                                {
							            <div>입찰이 종료되었습니다.</div>
                                }
                            }
                        }					
						
						<div style="clear:both;">
						</div>
					</div>

                    <div style="background-image:url(@Url.Content("~/images/detail_info_2.png")); width:550px; height:62px;margin-left:10px;float:left;" >
                        <div style="float:left;width:270px;text-align:center;">
							<span style="line-height:62px;"><strong>공구기간: </strong>@Util.nvl(Model.goods.JoinDateStart)~@Util.nvl(Model.goods.JoinDateEnd)</span>							
						</div>
						<div style="float:left;width:270px;text-align:center;">
							<span style="line-height:62px;"><strong>입찰기간: </strong>@Util.nvl(Model.goods.BidDateStart)~@Util.nvl(Model.goods.BidDateEnd)</span>
						</div>
					 </div>
					 <div style="float:left;height:100%;margin-left:5px;">
						<a href="#" onclick="ShareUrl()"><img src="@Url.Content("~/images/dig.png")" alt="퍼가기버튼" width="86" height="62"></a>
					 </div>
				</div>	
				<div style="clear:both;"></div>
				<div>
					<hr style="color:#ebebeb; height:1px; width:100%" />
				</div>
			</div>
			<div style="border: 1px solid #ebebeb; width:870px;height:100%;margin:25px;font-size:10pt;">                
                @Html.Raw(Model.goods.Detail)
			</div>
			<div style="margin:25px;">
				<textarea rows="2" id="commentText" placeholder="@(data.UserSeq == 0 ? "로그인 후 사용하세요." : "")"></textarea>
				<button id="comment" class="btn btn-midium btn btn-inverse" type="button">확인</button>
			</div>
			<div>
				<hr style="color:#02c08a; height:2px; width:100%; " />
			</div>
            <div id="comment_container">
			<!-------------한줄 메세지        --------------------------------->
            @foreach (BuyThePeople.Models.T_COMMENT c in Model.commentList)
            {
			<div class="messages">
				<div class="nickname">
					<span>@(c.T_USER.Name)( @Util.nvl(c.RegDate) )</span>
				</div>
				<div class="comment">
					<span>
						@Html.Raw(c.Comment.Replace("^$%", "<br />"))
					</span>
				</div>
				<div>
					<hr style="border:1px dashed #b9d5dd; width:100%;"/>
				</div>			
			</div>
            }
            </div>			
		</div>
    </div>	

<script src="@Url.Content("~/Scripts/API.js")" type="text/javascript"></script>
<script type="text/javascript">
    (function () {

        var join = document.getElementById("join");
        if (join) {
            join.onclick = function () {
                $.ajax({
                    url: "/Goods/Join/@Model.goods.GoodsSeq",
                    success: function (json, textStatus, jqXHR) {
                        var data = eval("(" + json + ")");
                        if (data.count < 0) {
                            alert(data.msg);
                            if (data.count == -2) {
                                document.location.href = "/User/Login?url=@ViewBag.url";
                            }
                        }
                        else {
                            alert(data.msg);
                            $("#joinCount").html(data.count);
                        }
                    },
                    error: function () {
                        alert("err");
                    }
                });
            };
        }

        var bid = document.getElementById("bid");
        if (bid) {
            bid.onclick = function () {

                var price_min = $("#price_min").html().replaceAll(",", "");
                var price_max = $("#price_max").html().replaceAll(",", "");
                var price = $("#price").val();

                if (price < price_min || price > price_max) {
                    alert("입찰가격은 설정된 가격범위[" + price_min + "~" + price_max + "]에 포함되어야 합니다.");
                    return false;
                }

                $.ajax({
                    url: "/Goods/Bid/@Model.goods.GoodsSeq?price="+document.getElementById("price").value,
                    success: function (json, textStatus, jqXHR) {
                        var data = eval("(" + json + ")");
                        if (data.result > 0) {
                            alert(data.msg);
                            $("#price").val("");
                        }
                    },
                    error: function () {
                        alert("err");
                    }
                });
            };
        }

        document.getElementById("comment").onclick = function () {
            var value = document.getElementById("commentText").value;
            value = value.replace(/(^\s*)|(\s*$)/g, "").replace(/\r\n/ig, "^$%");
            
            $.ajax({
                url: "/Goods/Comment/@Model.goods.GoodsSeq?commentText="+encodeURIComponent(value),
                type: "POST",                
                success: function (json, textStatus, jqXHR) {
                    var data = eval("(" + json + ")");

                    if (data.result > 0) {
                        $("#comment_container").append('<div class="messages"><div class="nickname"><span>' + data.data.userName + '( ' + data.data.regDate + ' )</span></div><div class="comment"><span>' + data.data.comment.replaceAll("^$%", "<br />") + '</span></div><div><hr style="border:1px dashed #b9d5dd; width:100%;"/></div></div>');
                        $("#commentText").val("");
                    }
                    else {
                        alert(data.data);
                        document.location.href = "/User/Login?url=@ViewBag.url";
                    }
                },
                error: function () {
                    alert("err");
                }
            });
        };

    })();

    function ShareUrl() {
        
        DelegateGetShortUrl = function (val) {
            FBFeed(val,
                //'@Model.goods.Image',
                'http://kingtw2.cafe24.com/images/icon.png',
                document.title,
                '@Model.goods.Title');
        };

        GetShortUrl(document.location.href);
    }
</script>